Skip to content

fix(@angular/build): use stable worker filenames during dev server#32940

Open
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-30494-worker-hash-rebuild
Open

fix(@angular/build): use stable worker filenames during dev server#32940
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-30494-worker-hash-rebuild

Conversation

@maruthang
Copy link
Copy Markdown
Contributor

@maruthang maruthang commented Apr 6, 2026

Summary

  • During ng serve, web worker bundles always used entryNames: 'worker-[hash]' regardless of whether the parent build used hashing, causing the worker filename hash to change on every rebuild even when worker content was unchanged
  • The fix conditionally uses worker-[hash] only for production builds (when parent uses hashing) and worker-[name] for dev server, keeping worker filenames stable across rebuilds
  • Explicitly sets splitting: false and footer: undefined in the worker build to prevent inherited options from causing non-deterministic output

Closes #30494

Test plan

  • Existing worker rebuild tests pass with updated regex patterns
  • ng serve with a web worker no longer changes the worker filename on unrelated rebuilds
  • Production builds (ng build) still use hashed worker filenames for cache busting

… prevent hash changes on rebuild

During `ng serve`, web workers were always bundled with `entryNames: 'worker-[hash]'`
regardless of whether the parent build used hashing. Combined with inherited `footer`
and `splitting` options from the parent build that could change between rebuilds, this
caused the worker filename hash to change on every rebuild even when the worker content
was unchanged, breaking debugging sessions.

The fix conditionally uses `worker-[hash]` only when the parent build uses hashing
(production builds), and `worker-[name]` otherwise (dev server). It also explicitly
sets `splitting: false` and `footer: undefined` in the worker build to prevent
non-deterministic output that could affect hashing.

Closes angular#30494
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Angular application builder to support both hashed and non-hashed Web Worker filenames, which prevents debugging sessions from breaking during development rebuilds. The bundling logic is modified to conditionally apply hashing based on the parent build's configuration and disables features like splitting and footers to ensure deterministic output. The review feedback suggests refining the regular expressions used for matching worker filenames by replacing the broad '.+' pattern with a more specific '[\w-]+' character set to improve robustness.

*/
const REFERENCED_WORKER_REGEXP =
/new Worker\(new URL\("worker-[A-Z0-9]{8}\.js", import\.meta\.url\)/;
/new Worker\(new URL\("worker-.+\.js", import\.meta\.url\)/;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved robustness in this test, consider making the regular expression more specific. The .+ pattern is quite broad. Using [\w-]+ instead would restrict matches to alphanumeric characters, underscores, and hyphens, which more accurately reflects the expected worker filenames (worker-[name].js or worker-[hash].js).

Suggested change
/new Worker\(new URL\("worker-.+\.js", import\.meta\.url\)/;
/new Worker\(new URL\("worker-[\w-]+\.js", import\.meta\.url\)/;

*/
const REFERENCED_WORKER_REGEXP =
/new Worker\(new URL\("worker-[A-Z0-9]{8}\.js", import\.meta\.url\)/;
/new Worker\(new URL\("worker-.+\.js", import\.meta\.url\)/;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved robustness in this test, consider making the regular expression more specific. The .+ pattern is quite broad. Using [\w-]+ instead would restrict matches to alphanumeric characters, underscores, and hyphens, which more accurately reflects the expected worker filenames (worker-[name].js or worker-[hash].js).

Suggested change
/new Worker\(new URL\("worker-.+\.js", import\.meta\.url\)/;
/new Worker\(new URL\("worker-[\w-]+\.js", import\.meta\.url\)/;

// Match both hashed (worker-ABCD1234.js) and non-hashed (worker-<name>.js) patterns
const workerCodeFile = workerResult.outputFiles.find((file) =>
/^worker-[A-Z0-9]{8}.[cm]?js$/.test(path.basename(file.path)),
/^worker-.+\.[cm]?js$/.test(path.basename(file.path)),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The regex .+ is a bit too broad and could potentially match unexpected file names. For improved robustness, consider using a more specific character set that matches valid file name characters, such as [\w-]+. This would match alphanumeric characters, underscores, and hyphens, which should cover both [name] and [hash] replacements.

Suggested change
/^worker-.+\.[cm]?js$/.test(path.basename(file.path)),
/^worker-[\w-]+\.[cm]?js$/.test(path.basename(file.path)),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ng serve: The filename hash for Shared Workers changes on every rebuild, which constantly breaks the debugging session.

1 participant